import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter_taotaoshipin/http/dao/user_dao.dart';
import 'package:flutter_taotaoshipin/model/user_login_entity.dart';
import 'package:flutter_taotaoshipin/model/user_open_entity.dart';
import 'package:flutter_taotaoshipin/model/userinfo_center_entity.dart';
import 'package:flutter_taotaoshipin/pages/login/local_user_info.dart';
import 'package:flutter_taotaoshipin/pages/purchase/apple_iap_info.dart';
import 'package:flutter_taotaoshipin/util/global.dart';
import 'package:flutter_taotaoshipin/value/app_profile.dart';
import 'package:flutter_taotaoshipin/value/global_config.dart';
import 'package:flutter_taotaoshipin/value/sp_key.dart';
import 'package:get/get.dart';

class AppController extends GetxController {
  var _appProfile = SpUtil.getObj(
    SpKey.appProfile,
    (v) => AppProfile.fromJson(v as Map<String, dynamic>),
    defValue: AppProfile(),
  )!;
  var _userinfoCenterEntity = UserinfoCenterEntity();
  var _applePayInfo = InAppPurchaseInfo(payCode: 'apple_pay');
  var _localUserInfo = SpUtil.getObj<LocalUserInfo2>(
    SpKey.localUserInfo,
    (v) => LocalUserInfo2.fromJson(v as Map<String, dynamic>),
    defValue: LocalUserInfo2(),
  )!;
  var _csj = UserOpenCsj();
  var _lastWatchAdTime = SpUtil.getInt(SpKey.lastWatchAdTime)!;
  var _adTodayWatchCount =
      SpUtil.getInt(SpKey.adTodayWatchCount)!;
  var _userWatchCsReplyTime =
      SpUtil.getInt(SpKey.userWatchCsReplyTime)!;

  /// app配置信息
  AppProfile get appProfile => _appProfile;

  /// 用户中心信息
  UserinfoCenterEntity get userinfoCenterEntity =>
      _userinfoCenterEntity;

  /// 账户信息
  LocalUserInfo2 get localUserInfo => _localUserInfo;

  /// 苹果支付信息
  InAppPurchaseInfo get applePayInfo => _applePayInfo;

  /// 穿山甲
  UserOpenCsj get csj => _csj;

  /// 今日观看广告最大数量
  int adDayLimit = 0;

  /// 上次观看广告时间
  int get lastWatchAdTime => _lastWatchAdTime;

  /// 今天已经观看广告数量
  int get adTodayWatchCount => _adTodayWatchCount;

  /// 自动登录数据
  UserLoginPlay? userLoginPlay;
  // 用户观看回复的时间
  int get userWatchCsReplyTime => _userWatchCsReplyTime;

  updateAppProfile(AppProfile profile) {
    // 赋值
    _appProfile = profile;
    // 保存
    SpUtil.putObject(SpKey.appProfile, profile);
    // 更新
    update();
  }

  updateUserinfoCenter(UserinfoCenterEntity entity) {
    // 赋值
    _userinfoCenterEntity = entity;
    // 更新
    update();
  }

  updateLocaleUserinfo(LocalUserInfo2 userInfo) {
    // 赋值
    _localUserInfo = userInfo;
    // 刷新登录
    if (GlobalConfig.refreshLogin > 0) {
      Future.delayed(Duration(minutes: GlobalConfig.refreshLogin),
          () {
        UserDao.refreshLogin();
      });
    }
      // 保存
    SpUtil.putObject(SpKey.localUserInfo, userInfo);
    // 保存钥匙串
    Global.setLocalUserInfoKeychain(userInfo);
    // 更新
    update();
  }

  updateApplePayInfo(InAppPurchaseInfo iapInfo) {
    // 赋值
    _applePayInfo = iapInfo;
    // 更新
    update();
  }

  updateCsj(UserOpenCsj csj) {
    // 赋值
    _csj = csj;
    // 更新
    update();
  }

  /// 更新广告次数
  updateAdTodayLimit() {
    if (lastWatchAdTime != 0 &&
        !DateUtil.isToday(lastWatchAdTime)) {
      // 上次观看不是今日
      _adTodayWatchCount = 0;
    }
  }

  /// 今日已经观看广告数量增加
  increaseAdTodayLimit() {
    updateAdTodayLimit();
    // 更新今天观看数量
    _adTodayWatchCount += 1;
    SpUtil.putInt(SpKey.adTodayWatchCount, _adTodayWatchCount);
    // 更新广告观看时间
    _lastWatchAdTime = DateTime.now().millisecondsSinceEpoch;
    SpUtil.putInt(SpKey.lastWatchAdTime, _lastWatchAdTime);
  }

  /// 用户观看客服回复的时间
  updateUserWatchCsReplyTime() {
    // 赋值
    _userWatchCsReplyTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;
    // 保存
    SpUtil.putInt(
        SpKey.userWatchCsReplyTime, _userWatchCsReplyTime);
    // 更新
    update();
  }
}
